/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * Allows for pages to switch views easily by using the navigate method and
 * the action listener method to set the new view from e.g. a buttons id.
 *
 * Parameters can also be set in this bean for easy communiation between views.
 *
 * @author jonas
 */
public class ViewChangeBean {

    public static String MANGED_BEAN_NAME = "ViewChange";

    private boolean success;
    private String toView;

    private String param;

    @PostConstruct
    void init(){
        success = true;
        toView = "main";
    }

    void setSuccess(boolean s){
        success = s;
    }
    boolean getSuccess(){
        return success;
    }

    void setToView(String tv){
        toView = tv;
    }
    String getToView(){
        return toView;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public void listener(ActionEvent e){
        toView = e.getComponent().getId();
    }

    public void paramListener(ActionEvent e){
        FacesContext facesCont = FacesContext.getCurrentInstance();
        Map params = facesCont.getExternalContext().getRequestParameterMap();
        param = (String)params.get("param");

        System.out.println(param);

        toView = e.getComponent().getId();
    }

    public Object navigate(){
        
        if( success ){
            return toView;
        }else{
            return "failure";
        }
    }


}
